package ManHinhChinh;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

public class NhapHang {

	// Khai bao element
	protected Shell NhapHang;
	private Text txt_MaPNH;
	private Text txt_Soluong;
	private Text txt_DonGiaNhap;
	private Text txt_DVTinh;
	private Text txt_TongTinh;
	private Table table;
	private Combo cbNCC;
	private Button btnNhap;
	private Composite composite_HDNhap;
	private Composite composite_HangHoa;
	private Button btn_NH_Them;
	private Button btn_NH_Ghi;
	private Combo cbMatHang;
	private DateTime dtNgayNhap;
	protected DataUser _du;

	String sSelectfromNCC = "select * from NHA_CUNG_CAP"; // lấy thông tin nhà cung cấp
	String sSelectfromHH = "select * from HANG_HOA"; // lấy thông tin hàng hóa
	String sSelectfromPNH = "select * from PHIEU_NHAP_HANG";// lấy thông tin phiếu nhập hàng
		
	private TableColumn tblclmnSTT;
	private TableColumn tblclmnMHH;
	private TableColumn tblclmnTenMatHang;
	private TableColumn tblclmnSoLuong;
	private TableColumn tblclmnDonGiaNhap;
	private TableColumn tblclmnThanhTien;
	private TableColumn tblclmnnDVTinh;

	private boolean started;

	
	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();

		// canh giua man hinh
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = NhapHang.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		NhapHang.setLocation(x, y);

		NhapHang.open();
		NhapHang.layout();
		while (!NhapHang.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		NhapHang = new Shell();
		NhapHang.addShellListener(new ShellAdapter() {
			@Override
			public void shellActivated(ShellEvent e) {
				// lần đầu tiên: started = false -> sau lần đầu tiên started =
				// true
				if (!started) {
					NhapHang.setVisible(true);
					TaoKetNoi kn = new TaoKetNoi();
					Connection con = kn.TaoLienKet();

					// cbMatHang.removeAll(); // khong bi copy du lieu MatHang
					// cbNCC.removeAll(); // khong bi copy du lieu NCC

					Statement stm = null;
					ResultSet rs = null;
					_du = new DataUser();
					// hiển thị mã phiếu nhập hàng tự động
					txt_MaPNH.setText(_du.taoMaPNHtudong());
					try {
						stm = con.createStatement();
						rs = stm.executeQuery(sSelectfromNCC);
						while (rs.next()) {
							String key1 = rs.getString("TENNCC");
							cbNCC.add(key1);
							cbNCC.setData(key1, rs.getString("MANCC"));
						}
						rs = stm.executeQuery(sSelectfromHH);
						while (rs.next()) {
							String key2 = rs.getString("TENHANGHOA");
							cbMatHang.add(key2);
							cbMatHang.setData(key2, rs.getString("MAHANGHOA"));
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} finally {
						try {
							stm.close();
							rs.close();
							con.close();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					capnhatTable();
					started = true;
				}
			}
		});
		NhapHang.setSize(742, 606);
		NhapHang.setText("Nh\u1EADp H\u00E0ng");

		composite_HDNhap = new Composite(NhapHang, SWT.NONE);
		composite_HDNhap.setBounds(20, 10, 696, 163);

		Label lblNewLabel = new Label(composite_HDNhap, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblNewLabel.setBounds(26, 23, 97, 21);
		lblNewLabel.setText("Mã Phiếu Nhập");

		Label lblNewLabel_1 = new Label(composite_HDNhap, SWT.NONE);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblNewLabel_1.setBounds(26, 75, 97, 21);
		lblNewLabel_1.setText("Nh\u00E0 Cung C\u1EA5p");

		Label lblNewLabel_2 = new Label(composite_HDNhap, SWT.NONE);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblNewLabel_2.setBounds(352, 23, 97, 21);
		lblNewLabel_2.setText("Ng\u00E0y Nh\u1EADp");

		txt_MaPNH = new Text(composite_HDNhap, SWT.BORDER);
		txt_MaPNH.setBounds(131, 23, 169, 21);

		dtNgayNhap = new DateTime(composite_HDNhap, SWT.BORDER);
		dtNgayNhap.setBounds(455, 23, 121, 24);

		cbNCC = new Combo(composite_HDNhap, SWT.NONE | SWT.DROP_DOWN
				| SWT.READ_ONLY);
		cbNCC.setBounds(132, 72, 168, 23);

		btnNhap = new Button(composite_HDNhap, SWT.NONE);

		// sự kiện click btn nhập hàng
		btnNhap.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (txt_MaPNH.getText().equals("")) {
					MessageBox mb = new MessageBox(NhapHang, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Bạn chưa nhập Mã hóa đơn nhập.");
					mb.open();
					txt_MaPNH.forceFocus();
					return;
				}

				if (cbNCC.getText().equals("")) {
					MessageBox mb = new MessageBox(NhapHang, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Bạn chưa chọn nhà cung cấp.");
					mb.open();
					cbNCC.forceFocus();
					return;
				}

				TaoKetNoi kn = new TaoKetNoi();
				Connection con = kn.TaoLienKet();
				//Kiểm tra mã phiếu nhập	
				try {
					Statement stm = con.createStatement();
					ResultSet rs = stm.executeQuery(sSelectfromPNH);
					while (rs.next()) {
						if (txt_MaPNH.getText().equals(
								rs.getString("MAPNH").trim())) {
							MessageBox ms = new MessageBox(NhapHang, SWT.ERROR
									| SWT.OK);
							ms.setText("Error");
							ms.setMessage("Mã phiếu nhập hàng đã tồn tại.");
							ms.open();
							return;
						}
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				
				// INSERT thông tin phiếu nhập vào csdl PHIEU_NHAP_HANG
				PreparedStatement pstm;
				String sInsertPNH = "Insert into PHIEU_NHAP_HANG values(?,?,?, 0)";
				String MaNCC = (String)cbNCC.getData(cbNCC.getText()).toString().trim();
				String Ngaynhap = dtNgayNhap.toString().substring(10, dtNgayNhap.toString().length()-1);
				try {
					pstm = con.prepareStatement(sInsertPNH);
					pstm.setString(1, MaNCC);
					pstm.setString(2, txt_MaPNH.getText().trim());
					pstm.setString(3, Ngaynhap );
					pstm.executeUpdate();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				

				// enable khung nhap hang hoa phia duoi
				// disable khung nhap hoa don phia tren
				composite_HDNhap.setEnabled(false);
				composite_HangHoa.setEnabled(true);

				// Enable mot so nut
				btn_NH_Them.setEnabled(true);
				btn_NH_Ghi.setEnabled(true);
				//btn_NH_Hoan.setEnabled(true);
			}
		});
		btnNhap.setImage(SWTResourceManager
				.getImage("..\\_QuanLyBanHang\\image\\text.ico"));
		btnNhap.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.BOLD));
		btnNhap.setBounds(289, 112, 121, 35);
		btnNhap.setText("Nh\u1EADp H\u00E0ng");

		btn_NH_Them = new Button(NhapHang, SWT.NONE);
		
		//////////////////////////////////
		///////////////////// nhấn nút thêm
		////////////////////////////////////////////
		btn_NH_Them.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cbMatHang.clearSelection();
				txt_Soluong.setText("");
				txt_DonGiaNhap.setText("");
			}
		});

		btn_NH_Them.setImage(SWTResourceManager
				.getImage("..\\_QuanLyBanHang\\image\\ADD.ICO"));
		btn_NH_Them.setBounds(20, 532, 75, 25);
		btn_NH_Them.setText("Th\u00EAm");
		btn_NH_Them.setEnabled(false);

		btn_NH_Ghi = new Button(NhapHang, SWT.NONE);
		
		//////////////////////////////////////////////
		///////////////nhấn nút ghi dữ liệu
		//////////////////////////////////////////
		btn_NH_Ghi.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (cbMatHang.getText() == "") {
					MessageBox mb = new MessageBox(NhapHang, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Bạn chưa chọn mặt hàng.");
					mb.open();
					cbMatHang.forceFocus();
					return;
				}

				if (txt_Soluong.getText() == "") {
					MessageBox mb = new MessageBox(NhapHang, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Bạn chưa nhập số lượng.");
					mb.open();
					txt_Soluong.forceFocus();
					return;
				}
				
				// kiểm tra điều kiện số lượng nhập + điều kiện số lượng tồn
				String sql = "select * from THAM_SO";
				TaoKetNoi kn = new TaoKetNoi();
				Connection con = kn.TaoLienKet();
				int slNhaptoithieu = 0;
				int slNhaptoida = 0;
				int slTonduocnhap = 0;
				try {
					Statement stm = con.createStatement();
					ResultSet rs = stm.executeQuery(sql);
					while (rs.next()) {
						slNhaptoithieu = rs.getInt("SOLUONGNHAPTOITHIEU");
						slNhaptoida = rs.getInt("SOLUONGNHAPTOIDA");
						slTonduocnhap = rs.getInt("SOLUONGTONDUOCNHAP");
					}
					if(Integer.parseInt(txt_Soluong.getText().trim()) < slNhaptoithieu
							|| Integer.parseInt(txt_Soluong.getText().trim())> slNhaptoida)
					{
						MessageBox mb = new MessageBox(NhapHang, SWT.ICON_ERROR
								| SWT.OK);
						mb.setText("Error");
						mb.setMessage("Số lượng nhập trong khoảng [ " + Integer.toString(slNhaptoithieu)+ " - " + Integer.toString(slNhaptoida) + " ]");
						mb.open();
						txt_Soluong.forceFocus();
						return;
					}
					PreparedStatement pstm = con.prepareStatement("select * from HANG_HOA where TENHANGHOA = ? ");
					pstm.setString(1, cbMatHang.getText().trim());
					rs = pstm.executeQuery();
					while(rs.next())
					{
						if (rs.getInt("SOLUONGTON") > slTonduocnhap) {
							MessageBox mb = new MessageBox(NhapHang,
									SWT.ICON_ERROR | SWT.OK);
							mb.setText("Error");
							mb.setMessage("Không thể nhập mặt hàng có số lượng tồn lớn.");
							mb.open();
							cbMatHang.forceFocus();
							return;
						}
					}
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 

				if (txt_DonGiaNhap.getText() == "") {
					MessageBox mb = new MessageBox(NhapHang, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Bạn chưa nhập đơn giá.");
					mb.open();
					txt_DonGiaNhap.forceFocus();
					return;
				}
				
				//Update csdl
				// thêm vào bảng chi tiết phiếu nhập
				PreparedStatement pstm;
				String sInsertCTPNH = "insert into CHI_TIET_PNH values(?,?,?,?,?,?)";
				String MaNCC = (String)cbNCC.getData(cbNCC.getText()).toString().trim();
				String MaHH = (String)cbMatHang.getData(cbMatHang.getText()).toString().trim();
				int sluongnhap = Integer.parseInt(txt_Soluong.getText().trim());
				int dongianhap = Integer.parseInt(txt_DonGiaNhap.getText().trim());
				try {
					pstm = con.prepareStatement(sInsertCTPNH);
					pstm.setString(1,MaHH);
					pstm.setString(2, MaNCC);
					pstm.setString(3, txt_MaPNH.getText().trim());
					pstm.setInt(4, sluongnhap);
					pstm.setInt(5,dongianhap);
					pstm.setInt(6, sluongnhap*dongianhap);
					pstm.executeUpdate();
					
					//Cập nhật lại Số Lượng cho bảng HANG_HOA (thêm số lượng mặt hàng)
//					String updatesoluong = "update HANG_HOA set SOLUONGTON = SOLUONGTON + ? , DONGIADENGHI = ? where MAHANGHOA = ?";
//					PreparedStatement pStm2 = con.prepareStatement(updatesoluong);
//					pStm2.setInt(1, slTonduocnhap);
//					pStm2.setInt(2, dongianhap);
//					pStm2.setString(3, MaHH);
//					pStm2.executeUpdate();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				try {
					con.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				capnhatTable();
				tinhTongtien();
				MessageBox ms = new MessageBox(NhapHang, SWT.OK);
				ms.setText("");
				ms.setMessage("Đã lưu.");
				ms.open();
			}
		});
		btn_NH_Ghi.setImage(SWTResourceManager
				.getImage("..\\_QuanLyBanHang\\image\\Save.ico"));
		btn_NH_Ghi.setBounds(129, 532, 75, 25);
		btn_NH_Ghi.setText("Ghi");
		btn_NH_Ghi.setEnabled(false);

		// button thoat
		Button btn_NH_thoat = new Button(NhapHang, SWT.NONE);
		btn_NH_thoat.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				NhapHang.close();
			}
		});
		btn_NH_thoat.setImage(SWTResourceManager
				.getImage("..\\_QuanLyBanHang\\image\\exit.ico"));
		btn_NH_thoat.setBounds(361, 532, 75, 25);
		btn_NH_thoat.setText("Tho\u00E1t");

		composite_HangHoa = new Composite(NhapHang, SWT.NONE);
		composite_HangHoa.setEnabled(false);
		composite_HangHoa.setBounds(20, 179, 696, 170);

		Label lblNewLabel_4 = new Label(composite_HangHoa, SWT.NONE);
		lblNewLabel_4.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblNewLabel_4.setBounds(31, 20, 72, 19);
		lblNewLabel_4.setText("M\u1EB7t H\u00E0ng");

		Label lblSLng = new Label(composite_HangHoa, SWT.NONE);
		lblSLng.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblSLng.setText("S\u1ED1 L\u01B0\u1EE3ng");
		lblSLng.setBounds(31, 70, 72, 25);

		Label lblnGi = new Label(composite_HangHoa, SWT.NONE);
		lblnGi.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblnGi.setText("Đơn Giá Nhập");
		lblnGi.setBounds(337, 70, 95, 25);

		Label lblnVTnh = new Label(composite_HangHoa, SWT.NONE);
		lblnVTnh.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblnVTnh.setText("\u0110\u01A1n V\u1ECB T\u00EDnh");
		lblnVTnh.setBounds(348, 20, 84, 19);

		Label lblTngTin = new Label(composite_HangHoa, SWT.NONE);
		lblTngTin.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.NORMAL));
		lblTngTin.setText("Tổng Tiền");
		lblTngTin.setBounds(185, 119, 84, 25);

		cbMatHang = new Combo(composite_HangHoa, SWT.NONE | SWT.DROP_DOWN
				| SWT.READ_ONLY);
		cbMatHang.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				TaoKetNoi kn = new TaoKetNoi();
				Connection con = kn.TaoLienKet();
				PreparedStatement pstm = null;
				ResultSet rs = null;

				try {
					String sql = "Select * from HANG_HOA where TENHANGHOA = ?";
					pstm = con.prepareStatement(sql);
					pstm.setString(1, cbMatHang.getText());
					rs = pstm.executeQuery();
					while (rs.next()) {
						txt_DVTinh.setText(rs.getNString("DONVITINH"));
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				finally {
					try {
						pstm.close();
						rs.close();
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		cbMatHang.setBounds(129, 16, 171, 23);

		txt_Soluong = new Text(composite_HangHoa, SWT.BORDER);

		// NUM_ONLY TEXT texbox số lượng
		txt_Soluong.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent arg0) {
				switch (arg0.keyCode) {
				case SWT.BS: // Backspace
				case SWT.DEL: // Delete
				case SWT.HOME: // Home
				case SWT.END: // End
				case SWT.ARROW_LEFT: // Left arrow
				case SWT.ARROW_RIGHT: // Right arrow
					return;
				}
				if (!Character.isDigit(arg0.character)) {  
		            arg0.doit = false;  // disallow the action  
		        }  

			}
		});
		txt_Soluong.setBounds(129, 70, 171, 21);

		txt_DonGiaNhap = new Text(composite_HangHoa, SWT.BORDER);

		// NUM_ONLY for textbox đơn giá nhập
		txt_DonGiaNhap.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent arg0) {
				switch (arg0.keyCode) {
				case SWT.BS: // Backspace
				case SWT.DEL: // Delete
				case SWT.HOME: // Home
				case SWT.END: // End
				case SWT.ARROW_LEFT: // Left arrow
				case SWT.ARROW_RIGHT: // Right arrow
					return;
				}
				if (!Character.isDigit(arg0.character)) {  
		            arg0.doit = false;  // disallow the action  
		        }  
			}
		});
		txt_DonGiaNhap.setBounds(452, 70, 191, 21);

		txt_DVTinh = new Text(composite_HangHoa, SWT.BORDER);
		txt_DVTinh.setBounds(452, 18, 191, 21);
		txt_DVTinh.setEnabled(false);

		txt_TongTinh = new Text(composite_HangHoa, SWT.BORDER);
		txt_TongTinh.setEditable(false);
		txt_TongTinh.setBounds(289, 119, 191, 21);

		table = new Table(NhapHang, SWT.BORDER | SWT.VIRTUAL | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
		table.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		table.setBounds(20, 373, 696, 136);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setItemCount(1);
		
		
		
		///////////////////////////////////////////////////////////////////////////
		// phải chuột để xóa item trong table
		///////////////////////////////////////////////////////////////////////////
		Menu menu = new Menu(NhapHang, SWT.POP_UP);
	    table.setMenu(menu);
	    MenuItem mitem = new MenuItem(menu, SWT.PUSH);
	    mitem.setText("Xóa");
	    mitem.addListener(SWT.Selection, new Listener() {
	      public void handleEvent(Event event) {
	        table.remove(table.getSelectionIndices());
	        String MaHH = (String)cbMatHang.getData(cbMatHang.getText()).toString().trim();
	        String delete = "Delete from CHI_TIET_PNH where (MAPNH = ?) and (MAHANGHOA = ?) ";
	        TaoKetNoi s = new TaoKetNoi();
			Connection con = s.TaoLienKet();
			try {
				PreparedStatement pStm = con.prepareStatement(delete);
				pStm.setString(1, txt_MaPNH.getText().trim());
				pStm.setString(2, MaHH);
				pStm.executeUpdate();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			capnhatTable();
			tinhTongtien();
	      }
	    });

		tblclmnSTT = new TableColumn(table, SWT.CENTER);
		tblclmnSTT.setWidth(90);
		tblclmnSTT.setText("Số thứ tự");

		tblclmnMHH = new TableColumn(table, SWT.CENTER);
		tblclmnMHH.setWidth(100);
		tblclmnMHH.setText("Mã mặt hàng");

		tblclmnTenMatHang = new TableColumn(table, SWT.CENTER);
		tblclmnTenMatHang.setWidth(100);
		tblclmnTenMatHang.setText("Tên mặt hàng");

		tblclmnnDVTinh = new TableColumn(table, SWT.CENTER);
		tblclmnnDVTinh.setWidth(100);
		tblclmnnDVTinh.setText("Đơn vị tính");

		tblclmnSoLuong = new TableColumn(table, SWT.CENTER);
		tblclmnSoLuong.setWidth(100);
		tblclmnSoLuong.setText("Số lượng");

		tblclmnDonGiaNhap = new TableColumn(table, SWT.CENTER);
		tblclmnDonGiaNhap.setWidth(100);
		tblclmnDonGiaNhap.setText("Đơn giá nhập");

		tblclmnThanhTien = new TableColumn(table, SWT.CENTER);
		tblclmnThanhTien.setWidth(100);
		tblclmnThanhTien.setText("Thành tiền");

	}
	
	public void capnhatTable() {
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		table.setItemCount(0);
		TaoKetNoi s = new TaoKetNoi();
		Connection con = s.TaoLienKet();
		int stt = 1;
		try {
			PreparedStatement pstm = con
					.prepareStatement("Select CHI_TIET_PNH.MAHANGHOA, TENHANGHOA, DONVITINH, SOLUONGNHAP, DONGIANHAP, THANHTIENNHAP"
							+ " from CHI_TIET_PNH inner join HANG_HOA on CHI_TIET_PNH.MAHANGHOA = HANG_HOA.MAHANGHOA"
							+ " where MAPNH = ?");
			pstm.setString(1, txt_MaPNH.getText().trim());
			ResultSet resultSet = pstm.executeQuery();
			while (resultSet.next()) {

				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(new String[] { Integer.toString(stt),
						resultSet.getString("MAHANGHOA"),
						resultSet.getString("TENHANGHOA"),
						resultSet.getString("DONVITINH"),
						resultSet.getString("SOLUONGNHAP"),
						resultSet.getString("DONGIANHAP"),
						resultSet.getString("THANHTIENNHAP") });
				stt++;
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void tinhTongtien()
	{
		TaoKetNoi s = new TaoKetNoi();
		Connection con = s.TaoLienKet();
		double tongtien = 0;
		try {
			PreparedStatement pStm = con.prepareStatement("select TONGTIENPNH from PHIEU_NHAP_HANG where MAPNH = ?");
			pStm.setString(1,txt_MaPNH.getText().trim());
			ResultSet rs = pStm.executeQuery();
			while(rs.next())
			{
				tongtien = rs.getDouble("TONGTIENPNH");
			}
			txt_TongTinh.setText(Double.toString(tongtien));
//			pStm = con.prepareStatement("update HANG_HOA set TONGTIENPNH = ?");
//			pStm.setDouble(1, tongtien);
//			pStm.executeUpdate();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


